//
//  HJUserAccount.swift
//  Micro-blog Sina
//
//  Created by admin on 16/3/21.
//  Copyright © 2016年 admin. All rights reserved.
//

import UIKit

class HJUserAccount: NSObject, NSCoding {
    var access_token: String?
    var expires_in: NSTimeInterval = 0 {
        didSet {
            expiresDate = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    var uid: String?
    
    var expiresDate: NSDate?
    
    // The dictionary turn model, KVC
    init(dict: [String: AnyObject]) {
        super.init()
        self.setValuesForKeysWithDictionary(dict)
    }
    override var description: String {
        get{
            let key = ["access_token",  "uid", "expires_in", "expiresDate"]
            return dictionaryWithValuesForKeys(key).description
        }
    }
    // When the dictionary 'key' in the model can not find the corresponding attribute, the need to achieve this method will not be reported to the wrong
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    // MARK: - User message
    var screen_name: String?
    var avatar_large: String?
    
    // MARK: - 归档和解档
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
    }
    // 解档
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        uid = aDecoder.decodeObjectForKey("uid") as? String
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
    }
    
}
